#!/usr/bin/env python3

import socket
import os
import os.path
import time


code = {'1':'.----',
        '2':'..---',
        '3':'...--',
        '4':'....-',
        '5':'.....',
        '6’:‘-....',
        '7':’--...',
        '8':’---..',
        '9':’----.',
        '0':'-----'}
dt = 0.1

pwLED = os.open('/sys/class/leds/LED2/brightness',os.O_RDWR)
greenLED = os.open('/sys/class/leds/status_led/brightness',os.O_RDWR)
os.write(greenLED,'0'.encode())
def get_ip_address():
  ip =''
  try:
    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    s.connect(("8.8.8.8",80))
    ip=s.getsockname()[0]
  except OSError:
    print('NO IP address')
    time.sleep(10)
  finally:
    s.close()
  return ip
def di():
  os.write(pwLED,'0'.encode())
  time.sleep(dt)
  os.write(pwLED,'1'.encode())
  time.sleep(dt)
  os.write(pwLED,'0'.encode())
def da():
  os.write(pwLED,'0'.encode())
  time.sleep(dt)
  os.write(pwLED,'1'.encode())
  time.sleep(3*dt)
  os.write(pwLED,'0'.encode())

def sendCode(text):
  for c in text:
    if c>='0' and c<='9':
      for sound in code[c]:
        if sound=='.':
          di()
        if sound=='-':
          da()
      time.sleep(5*dt)
      print(code[c])
ipaddress = get_ip_address()
sendCode(ipaddress)

greenLED = os.open('/sys/class/leds/status_led/trigger',os.O_RDWR)
os.write(greenLED,'heartbeat'.encode())
